package com.shsh.safemanage.action;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.shsh.safemanage.dao.DaoCreator;
import com.shsh.safemanage.dao.DepartmentDao;
import com.shsh.safemanage.dao.EquipmentDao;
import com.shsh.safemanage.dao.PostCheckDao;
import com.shsh.safemanage.dao.TitlePostManageGroupDao;
import com.shsh.safemanage.dao.UserDao;
import com.shsh.safemanage.model.Department1;
import com.shsh.safemanage.model.Department2;
import com.shsh.safemanage.model.Department3;
import com.shsh.safemanage.model.Equipment;
import com.shsh.safemanage.model.JobPost;
import com.shsh.safemanage.model.ManageGroup;
import com.shsh.safemanage.model.PostCheck;
import com.shsh.safemanage.model.User;
import com.shsh.safemanage.util.Constants;

public class UserAction {
	public Boolean login(HttpServletRequest request) {
		String jobNum = request.getParameter(Constants.REQUEST_PARAMETER_JOB_NUM);
		String password = request.getParameter(Constants.REQUEST_PARAMETER_PASSWORD);
		String loginType = request.getParameter(Constants.REQUEST_PARAMETER_LOGINTYPE);
		
		UserDao userDao = DaoCreator.createUserDao();
		User user = null;
		if (loginType != null) {
			if (loginType.equals("1")) {
				user = userDao.login(jobNum, password);
			} else {
			}
		}
		
		if (user == null) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_LOGIN_FAILED);
			return false;
		} else {
			HttpSession session = request.getSession();
			session.setAttribute(Constants.SESSION_USER, user);
			
			return true;
		}
	}
	
	public boolean refreshUserInfo(HttpServletRequest request) {
		String nameStr = request.getParameter(Constants.REQUEST_PARAMETER_NAME);
		String jobNumStr = request.getParameter(Constants.REQUEST_PARAMETER_JOB_NUM);
		
		if ((nameStr == null || nameStr.length() == 0) && (jobNumStr == null || jobNumStr.length() == 0)) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_EMPTY_USERNAME_OR_JOBNUM);
			return false;
		}
		
		UserDao userDao = DaoCreator.createUserDao();
		User user = userDao.getUser(nameStr, jobNumStr);
		if (user == null) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_NULL_USER);
			return false;
		}
		
		if (user.isRegistered()) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_USER_HAS_REGISTERED);
			return false;
		}
		
		HttpSession session = request.getSession();
		session.setAttribute(Constants.SESSION_REGISTER_USER, user);
		
		return true;
	}
	
	public boolean chooseMappingShiyebu(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Set<Department1> set = (Set<Department1>) session.getAttribute(Constants.SESSION_MAPPING_SHIYEBU);
		if (set == null) {
			set = new HashSet<Department1>();
			session.setAttribute(Constants.SESSION_MAPPING_SHIYEBU, set);
		}
		
		String idStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
		try {
			Long id = new Long(idStr);
			DepartmentDao departmentDao = DaoCreator.createDepartmentDao();
			Department1 department1 = departmentDao.getDepartment1ByID(id);
			set.add(department1);
			
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public boolean chooseMappingEquipment1(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Set<Equipment> set = (Set<Equipment>) session.getAttribute(Constants.SESSION_MAPPING_EQUIPMENT);
		if (set == null) {
			set = new HashSet<Equipment>();
			session.setAttribute(Constants.SESSION_MAPPING_EQUIPMENT, set);
		}
		
		String idStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
		String isManagerStr = request.getParameter(Constants.REQUEST_PARAMETER_IS_MANAGER);
		try {
			Long id = new Long(idStr);
			EquipmentDao equipmentDao = DaoCreator.createEquipmentDao();
			Equipment equipment = equipmentDao.getEquipment(id);
			if (isManagerStr != null && isManagerStr.equals("1")) {
				equipment.setEquipmentManager(true);
			} else {
				equipment.setEquipmentManager(false);
			}
			set.add(equipment);
			
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public boolean chooseMappingEquipment2(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Set<Equipment> set = (Set<Equipment>) session.getAttribute(Constants.SESSION_MAPPING_EQUIPMENT2);
		if (set == null) {
			set = new HashSet<Equipment>();
			session.setAttribute(Constants.SESSION_MAPPING_EQUIPMENT2, set);
		}
		
		String idStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
		String isManagerStr = request.getParameter(Constants.REQUEST_PARAMETER_IS_MANAGER);
		try {
			Long id = new Long(idStr);
			EquipmentDao equipmentDao = DaoCreator.createEquipmentDao();
			Equipment equipment = equipmentDao.getEquipment(id);
			if (isManagerStr != null && isManagerStr.equals("1")) {
				equipment.setEquipmentManager(true);
			} else {
				equipment.setEquipmentManager(false);
			}
			set.add(equipment);
			
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public boolean chooseMappingManageGroup(HttpServletRequest request) {
		HttpSession session = request.getSession();
		
		User registerUser = (User) session.getAttribute(Constants.SESSION_REGISTER_USER);
		if (registerUser == null) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_NULL_REGISTER_USER);
			return false;
		}
		
		String idStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
		try {
			Long id = new Long(idStr);
			TitlePostManageGroupDao titlePostManageGroupDao = DaoCreator.createTitlePostManageGroupDao();
			ManageGroup manageGroup = titlePostManageGroupDao.getManageGroupByID(id);
			
			registerUser = (User) session.getAttribute(Constants.SESSION_REGISTER_USER);
			registerUser.setManageGroupID(manageGroup.getId());
			registerUser.setManageGroupName(manageGroup.getManageGroupName());
			registerUser.setModified(true);
			
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean chooseMappingPostcheck(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Set<PostCheck> set = (Set<PostCheck>) session.getAttribute(Constants.SESSION_MAPPING_POSTCHECK_SET);
		if (set == null) {
			set = new HashSet<PostCheck>();
			session.setAttribute(Constants.SESSION_MAPPING_POSTCHECK_SET, set);
		}
		
		String idStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
		try {
			Long id = new Long(idStr);
			PostCheckDao postCheckDao = DaoCreator.createPostCheckDao();
			PostCheck postCheck = postCheckDao.getPostCheck(id);
			set.add(postCheck);
			
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean chooseDepartment123(HttpServletRequest request) {
		HttpSession session = request.getSession();
		User registerUser = (User) session.getAttribute(Constants.SESSION_REGISTER_USER);
		if (registerUser == null) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_NULL_REGISTER_USER);
			return false;
		}
		
		String depart1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT1ID);
		String depart2IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT2ID);
		String depart3IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT3ID);
		
		DepartmentDao departmentDao = DaoCreator.createDepartmentDao();
		
		try {
			Long depart1ID = new Long(depart1IDStr);
			Long depart2ID = new Long(depart2IDStr);
			Long depart3ID = new Long(depart3IDStr);
			
			Department1 department1 = departmentDao.getDepartment1ByID(depart1ID);
			if (department1 == null) {
				request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_NULL_REGISTER_USER);
				return false;
			}
			
			Department2 department2 = departmentDao.getDepartment2ByID(depart2ID);
			if (department2 == null) {
				request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_NULL_REGISTER_USER);
				return false;
			}
			
			Department3 department3 = departmentDao.getDepartment3ByID(depart3ID);
			if (department3 == null) {
				request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_NULL_REGISTER_USER);
				return false;
			}
			
			registerUser.setDepartment1ID(depart1ID);
			registerUser.setDepartment1Name(department1.getDepartmentName());
			registerUser.setDepartment2ID(depart2ID);
			registerUser.setDepartment2Name(department2.getDepartment2Name());
			registerUser.setDepartment3ID(depart3ID);
			registerUser.setDepartment3Name(department3.getDepartment3Name());
			registerUser.setModified(true);
			
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean choosePost(HttpServletRequest request) {
		HttpSession session = request.getSession();
		
		User registerUser = (User) session.getAttribute(Constants.SESSION_REGISTER_USER);
		if (registerUser == null) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_NULL_REGISTER_USER);
			return false;
		}
		
		TitlePostManageGroupDao titlePostManageGroupDao = DaoCreator.createTitlePostManageGroupDao();
		
		String postIDStr = request.getParameter(Constants.REQUEST_PARAMETER_POSTID);
		try {
			Long postID = new Long(postIDStr);
			
			JobPost jobPost = titlePostManageGroupDao.getJobPostByID(postID);
			if (jobPost == null) {
				return false;
			}
			
			if (registerUser.getJobPost1ID() == null || registerUser.getJobPost1ID().equals(0L)) {
				registerUser.setJobPost1ID(postID);
				registerUser.setJobPost1Name(jobPost.getPostName());
				
				registerUser.setModified(true);
			} else if (registerUser.getJobPost2ID() == null || registerUser.getJobPost2ID().equals(0L)) {
				if (registerUser.getJobPost1ID().equals(postID)) {
					return true;
				}
				registerUser.setJobPost2ID(postID);
				registerUser.setJobPost2Name(jobPost.getPostName());
				
				registerUser.setModified(true);
			} else if (registerUser.getJobPost3ID() == null || registerUser.getJobPost3ID().equals(0L)) {
				if (registerUser.getJobPost1ID().equals(postID) || registerUser.getJobPost2ID().equals(postID)) {
					return true;
				}
				registerUser.setJobPost3ID(postID);
				registerUser.setJobPost3Name(jobPost.getPostName());
				
				registerUser.setModified(true);
			}
			
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean register(HttpServletRequest request) {
		HttpSession session = request.getSession();
		
		String password = request.getParameter(Constants.REQUEST_PARAMETER_PASSWORD);
		String confirmPassword = request.getParameter(Constants.REQUEST_PARAMETER_CONFIRM_PASSWORD);
		
		if (password == null || confirmPassword == null) {
			return false;
		}
		
		if (!password.equals(confirmPassword)) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_NOT_EQUAL_PASSWORDS);
			return false;
		}
		
		User registerUser = (User) session.getAttribute(Constants.SESSION_REGISTER_USER);
		registerUser.setPassword(password);
		UserDao userDao = DaoCreator.createUserDao();
		boolean isSuccess = userDao.updateUserInfo(registerUser);
		if (isSuccess) {
			registerUser.setModified(false);
			registerUser.setRegistered(true);
		}
		
		// update shiyebu mapping
		Set<Department1> department1Set = (Set<Department1>) session.getAttribute(Constants.SESSION_MAPPING_SHIYEBU);
		if (department1Set != null && department1Set.size() > 0) {
			DepartmentDao departmentDao = DaoCreator.createDepartmentDao();
			departmentDao.addUserDepartment1Mapping(registerUser, department1Set);
		}
		
		// update equipment mapping
		Set<Equipment> equipmentSet = (Set<Equipment>) session.getAttribute(Constants.SESSION_MAPPING_EQUIPMENT);
		if (equipmentSet != null && equipmentSet.size() > 0) {
			EquipmentDao equipmentDao = DaoCreator.createEquipmentDao();
			equipmentDao.addUserEquipmentMapping(registerUser, equipmentSet);
		}
		
		// update equipment1 mapping
		Set<Equipment> equipment2Set = (Set<Equipment>) session.getAttribute(Constants.SESSION_MAPPING_EQUIPMENT2);
		if (equipment2Set != null && equipment2Set.size() > 0) {
			EquipmentDao equipmentDao = DaoCreator.createEquipmentDao();
			equipmentDao.addUserEquipmentMapping(registerUser, equipment2Set);
		}
		
		// update postcheck mapping
		Set<PostCheck> postCheckSet = (Set<PostCheck>) session.getAttribute(Constants.SESSION_MAPPING_POSTCHECK_SET);
		if (postCheckSet != null && postCheckSet.size() > 0) {
			PostCheckDao postCheckDao = DaoCreator.createPostCheckDao();
			postCheckDao.addUserPostcheckMapping(registerUser, postCheckSet);
			
			if (registerUser.getManageGroupID() != null) {
				TitlePostManageGroupDao titlePostManageGroupDao = DaoCreator.createTitlePostManageGroupDao();
				titlePostManageGroupDao.addManagegroupPostcheckMapping(registerUser.getManageGroupID(), postCheckSet);
			}
		}
		
		session.setAttribute(Constants.SESSION_REGISTER_USER, null);
		
		return true;
	}
}
